﻿using System;
using System.Linq.Expressions;

namespace GdNet.Core.Assistants
{
    public static class ObjectAssistant
    {
        public static string GetPropertyName<T>(Expression<Func<T>> property)
        {
            return GetMemberNameByExpression(property.Body);
        }

        public static string GetPropertyName<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> property)
        {
            return GetMemberNameByExpression(property.Body);
        }

        private static string GetMemberNameByExpression(Expression propertyBody)
        {
            MemberExpression member = propertyBody as MemberExpression;

            if (member == null)
            {
                UnaryExpression unary = propertyBody as UnaryExpression;
                member = unary.Operand as MemberExpression;
            }

            return member.Member.Name;
        }
    }
}
